package com.youmingxing.adpter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.youmingxing.R;
import com.youmingxing.activity.selfInfo.PersonalInfoActivity;
import com.youmingxing.bean.Comment;
import com.youmingxing.network.AllStarApi;
import com.youmingxing.network.BaseJsonHandler;
import com.youmingxing.utils.LogUtil;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import jp.wasabeef.glide.transformations.CropCircleTransformation;


/**
 * Created by chihiro on 15/9/2.
 */
public class CommentsAdapter extends BaseAdapter {
    private Context context;
    private List<Comment> mList = new ArrayList<>();
    private LayoutInflater inflater;
    private ThumbUpListener thumbUpListener;


    public CommentsAdapter(ThumbUpListener listener, Context context) {
        this.context = context;
        this.inflater = LayoutInflater.from(context);
        thumbUpListener = listener;
    }

    public void setData(List<Comment> list) {
        if (list == null)
            return;
        if (mList == null) {
            mList = list;
        } else {
            mList.clear();
            mList.addAll(list);
        }
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if (mList == null || mList.size() == 0) {
            return 0;
        } else {
            return mList.size();
        }
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }


    private class ViewHolder {
        TextView name;
        ImageView userImage;
        TextView comment;
        TextView likeNum;
        ImageView imageLike;
        ImageView imageUserAuth;
        ImageView authenticationImg;

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.list_item_comment, null);
            holder = new ViewHolder();
            holder.name = (TextView) convertView.findViewById(R.id.tv_name);
            holder.comment = (TextView) convertView.findViewById(R.id.tv_comments);
            holder.userImage = (ImageView) convertView.findViewById(R.id.img_avatar);
            holder.authenticationImg = (ImageView) convertView.findViewById(R.id.authentication_img);
            holder.likeNum = (TextView) convertView.findViewById(R.id.tv_like_num);
            holder.imageLike = (ImageView) convertView.findViewById(R.id.img_like);
            holder.imageUserAuth = (ImageView) convertView.findViewById(R.id.tv_identity);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();

        }

        CommentListener listener = new CommentListener(position);
        Comment comment = mList.get(position);
        holder.name.setText(comment.u_name);
        holder.comment.setText(comment.content);
        holder.likeNum.setText(comment.thumbsup + "");
        holder.imageLike.setOnClickListener(listener);
        if (comment.my_thumbsup) {
            holder.imageLike.setImageResource(R.mipmap.redheart);
        } else {
            holder.imageLike.setImageResource(R.mipmap.like_btn);
        }
//        if (!comment.u_auth) {
//            holder.imageUserAuth.setVisibility(View.INVISIBLE);
//        } else {
//
//        }

        holder.imageUserAuth.setVisibility(View.VISIBLE);
        Glide.with(context).load(AllStarApi.getUserMarkIcon
                (comment.user_type)).
                crossFade().into(holder.authenticationImg);

        LogUtil.i("-----user_type-","" +comment.user_type);

        Glide.with(context).load(comment.u_img)
                .placeholder(R.drawable.default_avatar).error(R.drawable.default_avatar)
                .bitmapTransform(new CropCircleTransformation(context)).crossFade().into
                (holder.userImage);
        holder.userImage.setOnClickListener(listener);
        return convertView;
    }

    private class CommentListener implements View.OnClickListener {

        private int position;

        public CommentListener(int position) {
            this.position = position;
        }

        @Override
        public void onClick(View v) {
            Intent intent;
            switch (v.getId()) {
                case R.id.img_avatar:
                    intent = new Intent(context, PersonalInfoActivity.class);
                    intent.putExtra("u_id", mList.get(position).u_id);
                    context.startActivity(intent);
                    break;
                case R.id.img_like:
                    final Comment comment = mList.get(position);
                    AllStarApi.thumbsUpComment(comment.id, comment.update_id, new
                                    BaseJsonHandler(context) {
                                        @Override
                                        public void onSuccessWithData(JSONObject response) {
                                            if (response.optJSONObject("data").optInt("result") == 1) {
                                                comment.my_thumbsup = true;
                                                comment.thumbsup++;
                                                notifyDataSetChanged();
                                                if (thumbUpListener != null) {
                                                    thumbUpListener.thumbUpSuccess();
                                                }
                                            } else if (response.optJSONObject("data").optInt("result") == 2) {
                                                if (thumbUpListener != null) {
                                                    thumbUpListener.thumbUpFailed("点赞过了");
                                                }
                                            } else {
                                                if (thumbUpListener != null) {
                                                    thumbUpListener.thumbUpFailed("出错了");
                                                }
                                            }
                                        }
                                    }


                    );
                    break;
                default:
                    break;
            }
        }
    }

    public interface ThumbUpListener {
        void thumbUpSuccess();

        void thumbUpFailed(String failReason);
    }
}
